import { useService } from "@/lib/Service";
import { App } from "antd";
import { useCallback } from "react";
import { useIntl } from "react-intl";

interface IListRef {
    current: {
        refreshData: () => void;
        showLoading: () => void;
        hideLoading: () => void;
    }
}

interface IUseDeleteOptions {
    data: any;
    endPoint: string;
    cancelEndPoint: string;
    updateEndPoint?: string;
    listRef: IListRef
}

interface IUpdateOpt {
    title?: string;
    message?: string;
    okText?: string;
    cancelText?: string;
}
export const useDelete = ({
    data,
    endPoint,
    cancelEndPoint,
    updateEndPoint,
    listRef
}: IUseDeleteOptions) => {
    const intl = useIntl();
    const app = App.useApp();
    const modal = app.modal;
    const message = app.message;
    const withServiceApi = useService();
    
    const deleteAction = useCallback((updateData: any) => {
        modal.confirm({
            title: intl.formatMessage({id:"admin.layout.modal.question"}),
            content: intl.formatMessage({id:"admin.layout.modal.delete.msg"}),
            okText: intl.formatMessage({id:"admin.layout.modal.confirm"}),
            cancelText: intl.formatMessage({id:"admin.layout.modal.cancel"}),
            onOk: () => {
                typeof listRef?.current?.showLoading === "function" && listRef.current?.showLoading();
                withServiceApi.send({
                    endPoint: endPoint,
                    data: {
                        ...data,
                        ...updateData,
                        status: -1
                    }
                }).then(() => {
                    typeof listRef?.current?.refreshData === "function" && listRef.current?.refreshData();
                    typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
                }).catch((err) => {
                    message.error(err.message || "未知错误");
                    console.error(err);
                    typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
                });
            }
         });
    }, [endPoint, data]);
    const cancelAction = useCallback((updateData: any, nextData?: any, opt?: IUpdateOpt) => {
        modal.confirm({
            title: opt?.title || intl.formatMessage({id:"admin.layout.modal.question"}),
            content: opt?.message ||intl.formatMessage({id:"admin.layout.modal.cancel.msg"}),
            okText: opt?.okText ||intl.formatMessage({id:"admin.layout.modal.confirm"}),
            cancelText: opt?.cancelText ||intl.formatMessage({id:"admin.layout.modal.cancel"}),
            onOk: () => {
                typeof listRef?.current?.showLoading === "function" && listRef.current?.showLoading();
                withServiceApi.send({
                    endPoint: cancelEndPoint,
                    data: {
                        ...data,
                        ...updateData,
                        status: 0,
                        ...(nextData || {})
                    }
                }).then(() => {
                    typeof listRef?.current?.refreshData === "function" && listRef.current?.refreshData();
                    typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
                }).catch((err) => {
                    message.error(err.message || "未知错误");
                    console.error(err);
                    typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
                });
            }
         });
    }, [cancelEndPoint, data]);
    const updateStatusAction = useCallback((updateData: any, nextData?: any) => {
        typeof listRef?.current?.showLoading === "function" && listRef.current?.showLoading();
        withServiceApi.send({
            endPoint: updateEndPoint || "",
            data: {
                ...data,
                ...updateData,
                ...(nextData || {})
            }
        }).then(() => {
            typeof listRef?.current?.refreshData === "function" && listRef.current?.refreshData();
            typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
        }).catch((err) => {
            message.error(err.message || "未知错误");
            console.error(err);
            typeof listRef?.current?.hideLoading === "function" && listRef.current?.hideLoading();
        });
    }, [updateEndPoint, data]);
    return {
        delete: deleteAction,
        cancel: cancelAction,
        update: updateStatusAction
    };
};